package com.cskaoyan.gateway.controller.shopping;

/**
 * 订单服务
 * 创建日期: 2022/01/20 16:48
 *
 * @author ghr
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.PageResponse;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

/**
 * 订单服务
 * 创建日期: 2022/01/19 22:58
 *
 * @author ghr
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class OrderController {
    @Reference(timeout = 10000, check = false)
    private OrderCoreService orderCoreService;

    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);

        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = orderCoreService.createOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("/order")
    public ResponseData orderList(Integer size, Integer page, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        OrderListRequest request = new OrderListRequest();
        request.setUserId(uid);
        request.setPage(page);
        request.setSize(size);

        OrderListResponse response = orderCoreService.orderList(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(response.getDetailInfoList());
            pageResponse.setTotal(response.getTotal());
            return new ResponseUtil<>().setData(pageResponse);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String orderId, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(orderId);

        OrderDetailResponse response = orderCoreService.orderDetail(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map map, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        String orderId = (String) map.get("orderId");
        CancelOrderRequest request = new CancelOrderRequest();
        request.setOrderId(orderId);

        CancelOrderResponse response = orderCoreService.cancelOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String orderId,HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(orderId);

        DeleteOrderResponse response = orderCoreService.deleteOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}